package cn.xuyang520.tyim.sys.config;

import javax.annotation.Resource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import cn.xuyang520.tyim.sys.service.TyQuartzJob_1;

//@Configuration
@PropertySource("classpath:cron.properties")
public class TyQuartzConfig {

	@Resource
	private Environment environment;

	// 定义任务类
	@Bean(name = "tyJob_1")
	public JobDetailFactoryBean job1() {
		JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
		factoryBean.setJobClass(TyQuartzJob_1.class);
		return factoryBean;
	}

	@Bean
	public CronTriggerFactoryBean cronTrigger_1() {
		CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
		String cron = environment.getProperty("testCron");
		cronTriggerFactoryBean.setCronExpression(cron);
		cronTriggerFactoryBean.setJobDetail(job1().getObject());
		// 设置延迟
		cronTriggerFactoryBean.setStartDelay(0);
		cronTriggerFactoryBean.setGroup("tygroup");
		cronTriggerFactoryBean.setName("trigger_1");
		return cronTriggerFactoryBean;
	}

	@Bean
	@Lazy(false)
	public SchedulerFactoryBean schedulerFactoryBean() {
		SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
		// QuartzScheduler延时启动20S，应用启动完后 QuartzScheduler 再启动
		schedulerFactoryBean.setStartupDelay(20);
		// s设置自动启动
		schedulerFactoryBean.setAutoStartup(true);
		schedulerFactoryBean.setTriggers(cronTrigger_1().getObject());
		return schedulerFactoryBean;
	}
}